var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var request = require('request');
var cheerio = require('cheerio');
var gbk = require('node-readability-cheerio');
//设置默认呈现引擎
app.set('view engine','html');

app.engine('.html',require('ejs').renderFile);
//设置静态资源请求地址
app.use(express.static(__dirname));
//for parsing application/json
app.use(bodyParser.json());
//for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: true}));
//for parsing multipart/form-data
app.use(multer());

app.get('/',function(req,res) {	
res.sendFile(__dirname+'/index.html')
});
app.post('/splwar',function(req,res) {
    var type = req.body.type;
    var num = req.body.num;
    if(type == 1) {//博客园
    request.post({url:'http://www.cnblogs.com',form:{"CategoryType":"SiteHome","ParentCategoryId":0,"CategoryId":808,"PageIndex":req.body.num,"ItemListActionName":"PostList"}},function(err,ress,body) {
     if (!err && ress.statusCode == 200) {
       $ = cheerio.load(body);
       var $h = '';
       var $x = $('#post_list h3');
       var $y = $('#post_list .post_item_summary');
       var $z = $('#post_list .post_item_foot');
       var $q = $('#post_list .diggnum');
       $q.prepend(' 推荐:');
       for(var i=0;i<$x.length;i++) {

          $z.eq(i).append($q.eq(i));
         $h += '<div class="bb">' + $x.eq(i).html()+"<br>"+
         $y.eq(i).contents().last()+$z.eq(i)+ '</div><br>';
       }
       res.send($h);
      } else {
        res.sendStatus(404);
      } 
    });
    } else if(type == 2) {//sefmentfault问答
      request('http://segmentfault.com/questions/newest?page='+num,function(err,response,body) {
        if(!err && response.statusCode == 200) {
          $ = cheerio.load(body);
          var $h = '';
          var $x = $('.question-stream .title a');
          var $y = $('.question-stream .author');
          var $z = $('.question-stream .answers');
          var $q = $('.question-stream .ib');
          $q.find('a').attr('href','#');      
          $x.attr('target','_blank');
          for(var i=0; i< $x.length;i++) {
             $x.eq(i).attr('href','http://segmentfault.com' + $x.eq(i).attr('href'));
            $h += '<div class="bb">' +$x.eq(i) + "<br>"+$y.eq(i).find("a").eq(0).html() +
            '&nbsp;'+$y.eq(i).find('a').eq(1).html() +
            "&nbsp;"+$z.eq(i).html() + $q.eq(i).html()+'</div><br>'; 
          }
          res.send($h);
        } else {
          res.sendStatus(404);
        }
      });
    } else if(type == 3) {//segmentfault文章
      request('http://segmentfault.com/blogs/newest?page='+req.body.num,function(err,response,body) {
        if(!err && response.statusCode == 200) {
          $ = cheerio.load(body);
          var $h = '';
          var $x = $('.blog-stream .title a');
          var $y = $('.blog-stream .wordbreak');
          var $z = $('.blog-stream .summary .author');
          $x.attr('target','_blank');
          for(var i=0; i<$x.length; i++) {
            $x.eq(i).attr('href','http://segmentfault.com' + $x.eq(i).attr('href'));
            $h += '<div class="bb">'+$x.eq(i) + '<br>' + $y.eq(i).html() +'<br>'+ 
            $z.eq(i).children().last().contents().eq(1).contents().last()+' 发布于'+
            $z.eq(i).children().last().contents().eq(6)+'</div><br>';
          }
          res.send($h);
        }
      })
    } else if(type == 4) {//w3cfuns
      gbk.read('http://www.w3cfuns.com/fenotes.php?mod=notes&do=index&page='+req.body.num,function(err,$) {
          var $h = '';
          var $x = $('.wp .plist .p_r h2');
          var $y = $('.wp .p_r p');
          var $z = $('.wp .plist .p_r h2 .uname a')
          for(var i=0; i<$x.length; i++) {
            $x.eq(i).children().last().attr('href','http://www.w3cfuns.com/'+
              $x.eq(i).children().last().attr('href'));
            $h += '<div class="bb">'+$x.eq(i).children().last() +'<br>'+ 
            $y.eq(i).html() +'<br>博主：'+ $z.eq(i).html()+'</div><br>';
          }
          res.send($h);
      });
    } else if(type == 5) {//csdn web
      request('http://blog.csdn.net/web/index.html?&page='+req.body.num,function(err,response,body){
        if(!err && response.statusCode == 200) {
          $ = cheerio.load(body);
          var $h = '';
          var $x = $('.main_center .blog_list h1');
          var $y = $('.main_center .blog_list dl dd');
          var $z = $('.main_center .blog_list .about_info');
          for(var i=0; i< $x.length; i++) {
            $h += "<div class='bb'"+  $x.eq(i) +'<br>'+ $y.eq(i).html()+ "<br>"+$z.eq(i).html()+'</div><br>';
          }
          res.send($h);
        }
      })
    } else if(type == 6) {//w3ctech
      request('http://www.w3ctech.com/',function(err,response,body) {
       if(!err && response.statusCode == 200) {
        $ = cheerio.load(body);
        var $h = '';
        var $x = $('.topic_list .topic_list_content .topic_title a');
        var $y = $('.topic_list .topic_list_content .topic_meta ul');
         $x.attr('target','_blank');
        for(var i=0; i<$x.length; i++) {
          $x.eq(i).attr('href','http://www.w3ctech.com'+$x.eq(i).attr('href'));
          $h += '<div class="bb">'+$x.eq(i)+ '<br>'+$y.eq(i).html()+'</div><br>';
        }
        res.send($h);
       } 
      });
    } else if(type == 7) {//web研究所
      request('http://www.web-tinker.com/',function(err,response,body) {
        if(!err && response.statusCode == 200) {
          $ = cheerio.load(body);
          var $h = '';
          var $x = $('.left li h3 a');
          var $y = $('.left li');
           $x.attr('target','_blank');
          for(var i=0;i<$x.length;i++) {
            $x.eq(i).attr('href','http://www.web-tinker.com' +$x.eq(i).attr('href'));
            $h +="<div class='bb'>"+ $x.eq(i)+ '<br>'+ $y.eq(i).children().last().children('span')+'</div><br>';
          }
          res.send($h);
        }
      });
    } else if(type == 8) {//jquery之家
        request('http://www.htmleaf.com/',function(err,response,body) {
            if(!err && response.statusCode == 200) {
                $ = cheerio.load(body);
                var $h = '';
                var $x = $('.plugthumb .plug-title a'); 
                var $y = $('.plugthumb .plug-description');
                var $z = $('.plugthumb .pull-right');
                $x.attr('target','_blank');
                for(var i=0; i<$x.length; i++) {
                  $h += '<div class="bb">'+$x.eq(i) + $y.eq(i) + $z.eq(i)+'</div><br>';
                }
                 res.send($h);
            }
        });
    } else if(type == 9) {
      request('http://tieba.baidu.com/f?kw=javascript&ie=utf-8&pn='+ (num-1)*50,function(err,response,body) {
          if(!err && response.statusCode == 200) {
            $ = cheerio.load(body);
            var $h = '';
            var $x = $('#thread_list > li.j_thread_list .threadlist_title a');
            var $y = $('#thread_list > li.j_thread_list .threadlist_abs');
            var $z = $('#thread_list > li.j_thread_list .threadlist_rep_num')
            for(var i=0; i<$y.length; i++) {
              $x.eq(i).attr('href','http://tieba.baidu.com'+$x.eq(i).attr('href'));
              $h += "<div class='bb'>"+$x.eq(i) + $y.eq(i)+ '回复数：'+$z.eq(i).html()+'</div><br>'
            } 
            res.send($h);
          } 
      })
    }

});

app.listen(process.env.PORT||80);
